/**
 * Copyright (C) 2016 inote.online
 * FileName:	DemoService.java
 * Created:		2016年12月4日 下午6:30:11
 */

package sui.jersey.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

import org.slf4j.Logger;

import com.alibaba.fastjson.JSON;

import sui.utils.logs.LogUtils;
import sui.utils.result.ResultFactory;
import sui.utils.result.ResultFactory.ResultEnum;

/**
 * <p>
 * Description: 
 * </p>
 * @author 	XQF SUI
 * @Created 2016年12月4日 下午6:30:11
 * @version 1.0
 */
@Path("demoService")
@Produces(MediaType.APPLICATION_JSON)
public class DemoService {

	Logger logger = LogUtils.get();

	// http://localhost:8080/jersey/api/demoService/say/nih1
	@GET
	@Path("say/{customized : [a-zA-Z][a-zA-Z_0-9]*}")	// 采用正则表达式限制访问
	public String say(@PathParam("customized") String customized) {

		logger.info("开始调用say方法...");

		return ResultFactory.createResultJSONString(ResultEnum.COMMON_SUCCESS, ResultFactory.createMap("data", customized));
	}
	
	// http://localhost:8080/jersey/api/demoService/queryParam?age=12&sex=女&hobby=看书,上网,听音乐
	// http://localhost:8080/jersey/api/demoService/queryParam
	@GET
	@Path("queryParam")
	public String queryParam(@DefaultValue("2") @QueryParam("age") Integer age,
			@DefaultValue("男") @QueryParam("sex") String sex,
			@DefaultValue("冲浪,爬山,马拉松") @QueryParam("hobby") List<String> hobby) {

		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("age", age);
		paramMap.put("sex", sex);
		paramMap.put("hobby", JSON.toJSONString(hobby));

		return ResultFactory.createResultJSONString(ResultEnum.COMMON_SUCCESS, paramMap);
	}
	
	// http://localhost:8080/jersey/api/demoService/formParam
	// form表单
	//		username:admin
	//		password:123456
	@POST
	@Path("formParam")
	@Consumes("application/x-www-form-urlencoded")
	public String formParam(@FormParam("username") String username, @FormParam("password") String password) {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("username", username);
		paramMap.put("password", password);
		
		return ResultFactory.createResultJSONString(ResultEnum.COMMON_SUCCESS, paramMap);
	}
	
	@GET
	@Path("uriParam")
	public String uriParam(@Context UriInfo uri) {
		
		MultivaluedMap<String, String> queryParams = uri.getQueryParameters();  
	    MultivaluedMap<String, String> pathParams = uri.getPathParameters();
		
	    Map<String, Object> paramMap = new HashMap<String, Object>();
	    
	    for (String key : queryParams.keySet()) {
	    	paramMap.put("queryParams_" + key, queryParams.get(key));
	    	System.out.println(queryParams.get(key));
		}
	    
	    for (String key : pathParams.keySet()) {
	    	paramMap.put("pathParams_" + key, pathParams.get(key));
			System.out.println(queryParams.get(key));
		}

	    paramMap.put("uri", JSON.toJSONString(uri));
	    
		return ResultFactory.createResultJSONString(ResultEnum.COMMON_SUCCESS, paramMap);
	}
}
